package com.atguigu.gmall.order.consumer;

import com.alibaba.fastjson.JSONObject;
import com.atguigu.gmall.model.enums.ProcessStatus;
import com.atguigu.gmall.mq.config.MqConst;
import com.atguigu.gmall.order.service.OrderInfoService;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.Map;

/**
 * @Author: 李旭
 * @Time: 15:44
 * @Description:
 */
@Component
public class OrderConsumer {

    @Autowired
    private OrderInfoService orderInfoService;

    //取消订单
    @RabbitListener(queues = MqConst.QUEUE_ORDER_CANCEL)
    public void cancelOrder(Long orderId, Message message, Channel channel){
        try {

            orderInfoService.cancelOrder(orderId);

            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    //更新订单信息
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = MqConst.QUEUE_PAYMENT_PAY,durable = "true",autoDelete = "false"),
            exchange = @Exchange(value = MqConst.EXCHANGE_DIRECT_PAYMENT_PAY),
            key = MqConst.ROUTING_PAYMENT_PAY
    ))
    public void updateOrder(Long orderId, Message message, Channel channel){
        try {
            System.out.println("更新订单之ID：" + orderId);
            //更新订单状态
            orderInfoService.updateOrder(orderId);

            //发消息通知仓库
            //发消息  MQ   库存微服务
            orderInfoService.sendOrderStatus(orderId);
            //简单的应答
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    //减库存结果通知
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = MqConst.QUEUE_WARE_ORDER,durable = "true",autoDelete = "false"),
            exchange = @Exchange(value = MqConst.EXCHANGE_DIRECT_WARE_ORDER),
            key = MqConst.ROUTING_WARE_ORDER
    ))
    public void descStock(String json, Message message, Channel channel){
        try {
            Map map = JSONObject.parseObject(json, Map.class);

            /*map.put("orderId", wareOrderTask.getOrderId());
            map.put("status"*/
            String orderId = (String) map.get("orderId");
            System.out.println("减库存结果通知订单ID：" + orderId);
       /*     status	状态： ‘DEDUCTED’  (已减库存)
                    状态：  ‘OUT_OF_STOCK’  (库存超卖)*/
            //判断是
            if("DEDUCTED".equals(map.get("status"))){
                //减库存成功了  修改订单的状态  进度状态
                orderInfoService.updateOrder(orderId, ProcessStatus.WAITING_DELEVER);
            }else{
                //减库失败了
                orderInfoService.updateOrder(orderId, ProcessStatus.STOCK_EXCEPTION);
            }
            //简单的应答
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
